package com.application;

import android.content.Context;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;

public abstract class Formula {
    private int current = 0;
    private View[] radio;
    private View[] text;
    private Spinner[] gen;
    public RadioGroup.OnCheckedChangeListener checkListener;
    private char[] type;
    private Context context;

    public Formula(View[] radioId, View[] textId, Spinner[] gen, char[] type, Context c) {
        radio = radioId;
        text = textId;
        this.gen = gen;
        this.type = type;
        context = c;
        checkListener = new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i != radio[current].getId()) {
                    text[current].setEnabled(true);
                    for (int count = 0; count < radio.length; count++) {
                        if (radio[count].getId() == i) {
                            text[count].setEnabled(false);
                            ((RadioButton) radio[current]).setChecked(false);
                            current = count;
                        }
                    }
                }
            }
        };
        generateSpinners();
        createTextWatcher();
        createSelectedListener();
    }

    private void generateSpinners() {
        for (int i = 0; i < gen.length; i++) {
            ArrayAdapter c = createAdapter(type[i]);
            gen[i].setAdapter(c);
        }
    }

    private ArrayAdapter createAdapter(char c) {
        ArrayAdapter result = null;
        switch (c) {
            case 't':
                CharSequence[] temperatureUnits = new CharSequence[3];
                temperatureUnits[0] = Html.fromHtml("<html><span>&deg</span>C</html>");
                temperatureUnits[1] = Html.fromHtml("<html><span>&deg</span>K</html>");
                temperatureUnits[2] = Html.fromHtml("<html><span>&deg</span>F</html>");
                result = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item, temperatureUnits);
                result.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;

            case 'v':
                CharSequence[] volumeUnits = new CharSequence[4];
                volumeUnits[0] = "L";
                volumeUnits[1] = "mL";
                volumeUnits[2] = Html.fromHtml("<html>cm<sup><small>3</small></sup></html>");
                volumeUnits[3] = Html.fromHtml("<html>m<sup><small>3</small></sup></html>");
                result = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item, volumeUnits);
                result.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
            case 'p':
                result = ArrayAdapter.createFromResource(context, R.array.pressure_units, android.R.layout.simple_spinner_item);
                result.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                break;
        }
        return result;
    }

    private void createSelectedListener() {
        AdapterView.OnItemSelectedListener itemSelectedListener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                for (int count = 0; count < gen.length; count++) {
                    if (gen[count] == adapterView)
                        act(count, gen[count]);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        };
        for (Spinner c : gen)
            c.setOnItemSelectedListener(itemSelectedListener);
    }

    private void createTextWatcher() {
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                act(getCurrent());
            }
        };
        for (View c : text) {
            ((EditText) c).addTextChangedListener(textWatcher);
        }
    }

    public int getCurrent() {
        return text[current].getId();
    }

    public RadioGroup.OnCheckedChangeListener getCheckedChangeListener() {
        return checkListener;
    }

    public abstract void act(int i, Spinner c);

    public abstract void act(int current);
}
